/**
 * 
 */
package ar.edu.unq.svnpermission.panels;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import ar.edu.unq.svnpermission.components.AutoAttacheablePanel;
import ar.edu.unq.svnpermission.domain.Permission;
import ar.edu.unq.svnpermission.provider.PermissionDataProvider;
import ar.edu.unq.svnpermission.ui.model.Folders;
import ar.edu.unq.svnpermission.ui.model.PermissionManager;
import ar.edu.unq.svnpermission.ui.model.Permissions;
import ar.edu.unq.svnpermission.ui.pages.NewFolderPage;
import ar.edu.unq.svnpermission.ui.pages.PermissionManagerPage;

/**
 * Panel utilizado para mostrar el detalle de una Carpeta
 * @author nicolas
 */
public class FolderDetailPanel extends AutoAttacheablePanel {
	private static final long serialVersionUID = 1L;
	private ModalWindow modalPopup;
	private AjaxFallbackDefaultDataTable<Permission> permissionsTable;	
	private ListView<Permission> permissionsListView;

	public FolderDetailPanel(String id,MarkupContainer container) {
		super(id,container);		
	}
	
	@Override
	public void build() {
		this.addFields();		
	}
	
	

	protected void addFields()
	{
		this.add(new Label("folderName"));	
		this.createPermissionModalPopup();
		this.addPermissionsTable();
		this.addDeleteLink();
		if(((Folders) this.getParent().getDefaultModelObject()).getSelectedFolder()==null)
			this.setVisible(false);
	}
	
	/**
	 * Se encarga la tabla de permisos
	 * @return Un AjaxFallbackDefaultDataTable<Permission>
	 * @author nicolas
	 */
	@SuppressWarnings("serial")
	private AjaxFallbackDefaultDataTable<Permission> createPermissionsTable()
	{
        List<IColumn<Permission>> columns = new ArrayList<IColumn<Permission>>();
        columns.add(new PropertyColumn<Permission>(new Model<String>("Membership"), "getWho"));
        columns.add(new PropertyColumn<Permission>(new Model<String>("Lectura"), "hasReadAccess"));
        columns.add(new PropertyColumn<Permission>(new Model<String>("Escritura"), "hasWriteAccess"));        
        columns.add(new AbstractColumn<Permission>(new Model<String>("Eliminar")) {           
            public void populateItem(Item<ICellPopulator<Permission>> item, String id, IModel<Permission> model) {  
                LinkPanel<Permission> delete = new LinkPanel<Permission>(id,model,"Eliminar") {
                    @Override 
                    public void onClick(AjaxRequestTarget art) {                        
                       new Permissions().remove(this.getModelObject());
                       FolderDetailPanel.this.permissionsTable.modelChanged();
                       art.add(FolderDetailPanel.this.permissionsTable);
                    } 
                }; 
                item.add(delete);                
            }  
        });        
        
        return new AjaxFallbackDefaultDataTable<Permission>("table", columns, new PermissionDataProvider(((Folders) this.getParent().getDefaultModelObject()).getSelectedFolder()), 5);
	}
	

	public void createPermissionModalPopup()
	{
        this.modalPopup = new ModalWindow("permissionModal",this.getDefaultModel());
        this.modalPopup.setCookieName("permission");

        this.modalPopup.setCloseButtonCallback(new ModalWindow.CloseButtonCallback()
        {
			private static final long serialVersionUID = 1L;

			public boolean onCloseButtonClicked(AjaxRequestTarget target)
            {
                return true;
            }
        });

        this.modalPopup.setWindowClosedCallback(new ModalWindow.WindowClosedCallback()
        {
			private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target)
            {	
				FolderDetailPanel.this.permissionsListView.modelChanged();
                target.add(FolderDetailPanel.this);
            }
        });


        this.add(this.modalPopup);
        this.add(new AjaxLink<Void>("showModalPermission")
        {
			private static final long serialVersionUID = 1L;

			@Override
            public void onClick(AjaxRequestTarget target)
            {
				Folders folder = (Folders) FolderDetailPanel.this.getParent().getDefaultModelObject();
				modalPopup.setContent(new PermissionPanel(modalPopup.getContentId(), FolderDetailPanel.this, modalPopup,new CompoundPropertyModel<Permissions>(new Permissions(folder.getSelectedFolder()))));
		        modalPopup.setTitle("Asignacion de Permisos sobre la carpeta" + folder.getSelectedFolder().getName());
                modalPopup.show(target);
            }
        });
		
	}
	
	private void addDeleteLink()
	{
        this.add(new Link("deleteFolder")
                {
        			private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						Folders folder = (Folders) FolderDetailPanel.this.getParent().getDefaultModelObject();
						folder.remove();
						setResponsePage(new PermissionManagerPage());
						
					}

                });		
	}
	
	private void addPermissionsTable()
	{
		this.permissionsListView = new ListView<Permission>("permissionsList") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Permission> item) {
				item.add(new Label("user",item.getModelObject().getWho().toString()));
				item.add(new Label("hasReadAccess",item.getModelObject().getHasReadAccess().toString()));
				item.add(new Label("hasWriteAccess",item.getModelObject().getHasWriteAccess().toString()));		
				item.add(new AjaxFallbackLink<Permission>("deletePermissionLink",new Model(item.getModelObject())) {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						new Permissions().remove(this.getModelObject());
						FolderDetailPanel.this.permissionsListView.modelChanged();		
						target.add(FolderDetailPanel.this);
					}
				});
			}
			
		};
		this.permissionsListView.setOutputMarkupId(true);
		this.add(this.permissionsListView);
	}
	
}
